import Vue from 'vue'
import Router from 'vue-router'
import axios from 'axios'
import {CONST} from "../js/utils";
Vue.use(Router)

var aa = new Router({
  routes: [
    {
      path: '/',
      component: () => import('@/components/layout/Layout'),
      redirect: '/gabriellepodium/UserInfo',
      // redirect: '/appoint/AppointInfo',
      children: [
        {
          path: '/gabriellepodium/UserInfo',
          name: 'UserInfo',
          component: () => import('@/components/gabriellepodium/UserInfo')
        },
        {
          path: '/gabriellepodium/AppointInfo',
          name: 'AppointInfo',
          component: () => import('@/components/gabriellepodium/AppointInfo')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/components/layout/Login'),
    }
  ]
})


aa.beforeEach((to, from, next) => {
  if (to.name == 'login') {
    next()
  } else {
      if(window.localStorage.getItem('isLogin-gabriellepodium')){
        //取项目的名字
       let projectName =to.path.split('/')[1];
        //给导航栏加pid,标识是那个项目
        axios.get(CONST.URL.FIND_PODIUMS)
          .then((result)=>{
            let projects = result.data.data;
            projects.forEach((item,index)=>{
              if(item.name===projectName){
                window.localStorage.setItem('pid',item.id)
              }
            })
            next()
          })

      }else{
        next('/login')
      }
    // next({
    //   path: '/login'
    // })
  }
})
export default aa
